package dao;

import bean.User;

import java.sql.*;
import utils.*;

public class UserDao
{
    // 查找注册用户
    public Boolean isLogin(User user)
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
// 获得数据的连接
            conn = DButils.getConnection();
// 获得 Statement 对象
            stmt = conn.createStatement();
// 执行 SQL 语句
            String sQL = "select * from jdbc.user where name = '" + user.getName()+
                    "' and password = '" +user.getPassword() +"'" ;
            rs = stmt.executeQuery(sQL);
//查找用户
            if(rs.next())
            {
                return true;
            }
        } catch (Exception e) { e.printStackTrace();}
        finally {
            try {
                DButils.release(rs, stmt, conn);
            } catch (SQLException e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }
    // 添加注册用户到数据库
    public boolean insert(User user)
    {
        Connection conn = null;
        Statement stmt = null;
        try {
// 获得数据的连接
            conn = DButils.getConnection();
// 获得 Statement 对象
            stmt = conn.createStatement();
// 执行 SQL 语句
            String SQL = "INSERT INTO jdbc.user(name,password,email) "
                    + "values('" + user.getName()+"','"
                    + user.getPassword()+"','"
                    + user.getEmail()+"')";
            int num = stmt.executeUpdate(SQL);
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception e) { e.printStackTrace();}
        finally {
            try {
                DButils.release(stmt, conn);
            } catch (SQLException e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }
}
